package org.test.client.ui.component.btn;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;

import org.test.client.ui.skinui.btn.MyButtonUI;
import org.test.client.ui.top.TopPanel;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIConstants;

public class DropdownToggleBtn extends JToggleButton {
	private DropdownToggleBtn thisBtn = this;
	
	private static String dropdownIconStr = "fa-caret-down";
	private static int dropiconSize = UIConstants.iconminsize;
	
	private boolean filledArea = false;
	private boolean opaque = false;
	private boolean requestFacus = true;
	private boolean useDefaultUI = false;
	
	private Icon ico = null;
	private String iconStr = null;
	private int iconw = 19;
	private String txt = null;
	private Font txtFont = new Font(Font.DIALOG,Font.PLAIN,14);

	private Color backColor = null;
	private Color rolloverColor = new Color(Integer.parseInt("178acc", 16));
	private Color pressedColor = rolloverColor;//new Color(Integer.parseInt("c0c0c0", 16));
	private Color iconColor = Color.WHITE;
	private Color foreColor = Color.WHITE;
	
	private BufferedImage drawDownIcon = null;
	private int preWidth = 130;
	
	private ButtonUI ui = MyButtonUI.createWhiteBtnUI(this);
	
	private JPopupMenu popMenu = null;
	private TopPanel tp = null;
	private MouseInListener mouseInListener = null;
	private ActionListener setBtnNormalListener = null;
	
	public DropdownToggleBtn(String iconStr, String txt,TopPanel tp){
		this.iconStr = iconStr;
		this.txt  = txt;
		this.tp = tp;
	}
	
	public DropdownToggleBtn doInit(){
		
		initMenu();
		
		this.setFont(txtFont);
		this.setBorder(null);
		this.setOpaque(opaque);
		if(iconStr!=null && iconStr.length()>0){
			ico = Icons.getIconByDesc(iconStr, iconw, iconColor);
			this.setIcon(ico);
		}
		this.setContentAreaFilled(filledArea);
		this.setRequestFocusEnabled(requestFacus);
		//目的是为 画一个 向下的三角流出空间
		this.setText(txt+"    ");
		this.setBackground(backColor);
		this.setForeground(foreColor);
		if(!useDefaultUI){
			this.setUI(new BasicButtonUI());
		}else{
			this.setUI(ui);
		}
		mouseInListener = new MouseInListener(this, rolloverColor,pressedColor);
		this.addMouseListener(mouseInListener);
		this.setPreferredSize(new Dimension(preWidth,UIConstants.topBtnHeigth));
		
		return this;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		BufferedImage bi = Icons.getImgByDesc(dropdownIconStr, dropiconSize, iconColor);
		
		Dimension d = this.getPreferredSize();
		int x = d.width-25;
		int y = d.height/2 - dropiconSize/2+2;
		g.drawImage(bi, x, y, dropiconSize, dropiconSize, null);
		
	}
	
	@Override
	public Insets getMargin() {
		Insets margin = super.getMargin();
		Insets margin2 =  null;
		int leftAndRight = 2;
		if (margin != null){
			margin2 = new Insets(margin.top, leftAndRight, margin.bottom, leftAndRight);
		}else{
			margin2 = new Insets(20, leftAndRight, 20, leftAndRight);
		}
		return margin2;
	}

	public void initMenu(){
		popMenu = new JPopupMenu() ;
		setBtnNormalListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionevent) {
				thisBtn.setSelected(false);
				thisBtn.setContentAreaFilled(false);
				thisBtn.setBackground(null);
//				thisBtn.getModel().setPressed(false);
			}
		};

		for(int i=0;i<10;i++){
			JMenuItem item = new JMenuItem("条目条目条目"+(i+1));
			item.addActionListener(setBtnNormalListener);
			popMenu.add(item);
		}
	}
	
	public static String getDropdownIconStr() {
		return dropdownIconStr;
	}

	public static void setDropdownIconStr(String dropdownIconStr) {
		DropdownToggleBtn.dropdownIconStr = dropdownIconStr;
	}

	public static int getDropiconSize() {
		return dropiconSize;
	}

	public static void setDropiconSize(int dropiconSize) {
		DropdownToggleBtn.dropiconSize = dropiconSize;
	}

	public boolean isFilledArea() {
		return filledArea;
	}

	public void setFilledArea(boolean filledArea) {
		this.filledArea = filledArea;
	}

	public boolean isOpaque() {
		return opaque;
	}

	public void setOpaque(boolean opaque) {
		this.opaque = opaque;
	}

	public Icon getIco() {
		return ico;
	}

	public void setIco(Icon ico) {
		this.ico = ico;
	}

	public String getTxt() {
		return txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}

	public String getIconStr() {
		return iconStr;
	}

	public void setIconStr(String iconStr) {
		this.iconStr = iconStr;
	}

	public int getIconw() {
		return iconw;
	}

	public void setIconw(int iconw) {
		this.iconw = iconw;
	}

	public Font getTxtFont() {
		return txtFont;
	}

	public void setTxtFont(Font txtFont) {
		this.txtFont = txtFont;
	}

	public Color getRolloverColor() {
		return rolloverColor;
	}

	public void setRolloverColor(Color rolloverColor) {
		this.rolloverColor = rolloverColor;
	}

	public Color getPressedColor() {
		return pressedColor;
	}

	public void setPressedColor(Color pressedColor) {
		this.pressedColor = pressedColor;
	}

	public Color getIconColor() {
		return iconColor;
	}

	public void setIconColor(Color iconColor) {
		this.iconColor = iconColor;
	}

	public Color getForeColor() {
		return foreColor;
	}

	public void setForeColor(Color foreColor) {
		this.foreColor = foreColor;
	}

	public BufferedImage getDrawDownIcon() {
		return drawDownIcon;
	}

	public void setDrawDownIcon(BufferedImage drawDownIcon) {
		this.drawDownIcon = drawDownIcon;
	}

	public int getPreWidth() {
		return preWidth;
	}

	public void setPreWidth(int preWidth) {
		this.preWidth = preWidth;
	}

	public boolean isUseDefaultUI() {
		return useDefaultUI;
	}

	public void setUseDefaultUI(boolean useDefaultUI) {
		this.useDefaultUI = useDefaultUI;
	}

	public Color getBackColor() {
		return backColor;
	}

	public void setBackColor(Color backColor) {
		this.backColor = backColor;
	}

	public boolean isRequestFacus() {
		return requestFacus;
	}

	public void setRequestFacus(boolean requestFacus) {
		this.requestFacus = requestFacus;
	}

	public ButtonUI getUi() {
		return ui;
	}

	public void setUi(ButtonUI ui) {
		this.ui = ui;
	}

	public JPopupMenu getPopMenu() {
		return popMenu;
	}

	public void setPopMenu(JPopupMenu popMenu) {
		this.popMenu = popMenu;
	}

	public TopPanel getTp() {
		return tp;
	}

	public void setTp(TopPanel tp) {
		this.tp = tp;
	}
	
	
}
